--HEADER Create DiaryEntry Predefined Question; Copy iPerform.DiaryEntry to UDS.Answer; Create FullText Index on UDS.DiaryEntry

INSERT INTO UDS.Question (Name, TypeId, AnswerTypeId)
VALUES (N'DiaryEntry', 8, 1)
DECLARE @DiaryEntryQId INT = IDENT_CURRENT('UDS.Question')

DECLARE @diaryId INT
DECLARE @newDiaryId INT
DECLARE diary_cursor CURSOR FAST_FORWARD FOR SELECT Id FROM iPerform.DiaryEntry

OPEN diary_cursor
FETCH NEXT FROM diary_cursor INTO @diaryId

while @@FETCH_STATUS = 0
BEGIN
	INSERT INTO UDS.Answer ([Value], [DateTime], [CreatedOn], [UserId], [QuestionId])
	SELECT Name, Created, Created, OwnerId, @DiaryEntryQId FROM iPerform.DiaryEntry
	WHERE Id = @diaryId

	SET @newDiaryId = IDENT_CURRENT('UDS.Answer')

	INSERT INTO UDS.DiaryEntry ([Id], [Text], [LastModified])
	SELECT @newDiaryId, [Text], [LastModified] FROM iPerform.DiaryEntry
	WHERE Id = @diaryId

	INSERT INTO UDS.AnswerToIdea ([AnswerId], [IdeaId])
	SELECT @newDiaryId, IdeaId FROM iPerform.DiaryEntryToIdea
	WHERE DiaryEntryId = @diaryId

	FETCH NEXT FROM diary_cursor INTO @diaryId
END

CLOSE diary_cursor
DEALLOCATE diary_cursor